﻿using Basic.FlowDAL;
using Basic.FlowModel.DB;
using Basic.FlowRemoteModel.NodeAuditEmp.Model;
using WeDonekRpc.Helper;

namespace Basic.FlowCollect.lmpl
{
    internal class NodeAuditEmpCollect : INodeAuditEmpCollect
    {
        private readonly IFlowNodeAuditEmpDAL _BasicDAL;

        public NodeAuditEmpCollect ( IFlowNodeAuditEmpDAL basicDAL )
        {
            this._BasicDAL = basicDAL;
        }
        public DBFlowNodeAuditEmp Get ( long id )
        {
            return this._BasicDAL.Get(id);
        }
        public Result GetByNodeId<Result> ( long nodeId ) where Result : class, new()
        {
            return this._BasicDAL.Get<Result>(a => a.NodeId == nodeId);
        }
        public bool Set ( NodeAuditEmp data )
        {
            DBFlowNodeAuditEmp sour = this._BasicDAL.Get<DBFlowNodeAuditEmp>(a => a.FlowId == data.FlowId && a.NodeId == data.NodeId);
            if ( sour == null )
            {
                this._BasicDAL.Add(data);
                return true;
            }
            return this._BasicDAL.Update(sour, data);
        }
        public Dictionary<long, string> GetShow ( long flowId )
        {
            return this._BasicDAL.Gets(a => a.FlowId == flowId, a => new
            {
                a.NodeId,
                a.Show
            }).ToDictionary(a => a.NodeId, a => a.Show);
        }
        public void Clear ( long flowId )
        {
            long[] ids = this._BasicDAL.Gets(a => a.FlowId == flowId, a => a.Id);
            if ( !ids.IsNull() )
            {
                this._BasicDAL.Delete(ids);
            }
        }
        public void Clear ( long flowId, long nodeId )
        {
            long id = this._BasicDAL.Get(a => a.FlowId == flowId && a.NodeId == nodeId, a => a.Id);
            if ( id != 0 )
            {
                this._BasicDAL.Delete(id);
            }
        }
        public Result[] Gets<Result> ( long flowId ) where Result : class, new()
        {
            return this._BasicDAL.Gets<Result>(a => a.FlowId == flowId);
        }
    }
}
